package android

import (
	"fmt"
	"github.com/beevik/etree"
	"log"
	"regexp"
	"strings"
)

var (
	CurrentPath      = `D:\JetBrains\workspace\go\src\soulAuto\resources\`
	AdbName          = `ld5.exe`
	SelectEles       []*etree.Element
	CheckResourceIds = make(map[string]func(ele *etree.Element)) // 需要检测的节点resources_id 及其 对应的处理方法
)

// AllEle 调用该函数的childELes为doc的根节点, key和value为需要匹配的节点
func AllEle(childEles []*etree.Element, key, value string) {
	for _, childEle := range childEles {
		id := childEle.SelectAttr(key)
		if id != nil {
			if f, ok := CheckResourceIds[id.Value]; ok {
				f(childEle)
				childEle.RemoveAttr("resource-id")
			}
			if id.Value == value {
				SelectEles = append(SelectEles, childEle)
			}
		}
		AllEle(childEle.ChildElements(), key, value)
	}
}

// GetEleBounds 获取ele坐标
func GetEleBounds(ele *etree.Element) (res []int) {
	if bounds := ele.SelectAttr("bounds"); bounds != nil {
		re := regexp.MustCompile(`\d+`)
		matches := re.FindAllString(bounds.Value, -1)
		if len(matches) != 4 {
			return
		}
		res = append(res, (sToi(matches[0])+sToi(matches[2]))/2, (sToi(matches[1])+sToi(matches[3]))/2,
			sToi(matches[0]), sToi(matches[1]), sToi(matches[2]), sToi(matches[3]))
	}
	return
}

// Tap 点击某个坐标
func Tap(bounds []int) {
	var call = fmt.Sprintf(`%s\%s -s 0 input tap %d %d`, CurrentPath, AdbName, bounds[0], bounds[1])
	res, e := ExecPowerCmdChina(call)
	Log.Println("tap", bounds, " success if empty: ", res, e)
}

// Swipe 滑动指定距离
func Swipe(bounds []int) {
	var call = fmt.Sprintf(`%s\%s -s 0 input swipe %v %v %v %v 1500`, CurrentPath, AdbName, bounds[0], bounds[1], bounds[2], bounds[3])
	res, e := ExecPowerCmdChina(call)
	Log.Println("Swipe ", bounds, res, " success if empty:", e)
}

// CheckKeyboard 检测当前模拟器键盘
func CheckKeyboard() string {
	var call = fmt.Sprintf(`%s\%s -s 0 ime set com.android.adbkeyboard/.AdbIME`, CurrentPath, AdbName)
	res, _ := ExecPowerCmdChina(call)
	if strings.Contains(res, "Input method com.android.adbkeyboard/.AdbIME selected") {
		Log.Println("adbkeyboard/.AdbIME selected")
		return ""
	}
	call = fmt.Sprintf(`%s\%s installapp --index 0 --filename '%s\ADBKeyboard.apk'`, CurrentPath, "dnconsole.exe", CurrentPath)
	res, e := ExecPowerCmdChina(call)
	if e != "" {
		Log.Println(e)
		return e
	}
	Log.Println("adbkeyboard/.AdbIME installapp")
	call = fmt.Sprintf(`%s\%s -s 0 ime set com.android.adbkeyboard/.AdbIME`, CurrentPath, AdbName)
	res, _ = ExecPowerCmdChina(call)
	if strings.Contains(res, "Input method com.android.adbkeyboard/.AdbIME selected") {
		Log.Println("adbkeyboard/.AdbIME set")
		return ""
	}
	Log.Println("please install ADBKeyboard.apk diy")
	return "please install ADBKeyboard.apk diy"
}

// KeyInput 输入信息
func KeyInput(msg string) {
	var call = fmt.Sprintf(`%s\%s -s 0 am broadcast -a ADB_INPUT_B64 --es msg '%s'`, CurrentPath, AdbName, Bs64(msg))
	_, e := ExecPowerCmdChina(call)
	Log.Println("Key Input:", msg, e)
}

// Dump 获取屏幕元素
func Dump() string {
	var call = fmt.Sprintf(`%s\%s -s 0 uiautomator dump`, CurrentPath, AdbName)
	res, e := ExecPowerCmdChina(call)
	if e != "" {
		log.Println(e)
		return ""
	}
	call = fmt.Sprintf(`%s\dnconsole.exe adb --index 0 --command 'pull /sdcard/window_dump.xml %s'`, CurrentPath, CurrentPath)
	log.Println(call)
	res, e = ExecPowerCmdChina(call)
	log.Println(res, e)
	return e
}

func ClearSelectEles() {
	SelectEles = []*etree.Element{}
}

// CurrentFocus 获取当前的活动界面
func CurrentFocus() string {
	var call = fmt.Sprintf(`%s\%s -s 0 'dumpsys window | grep CurrentFocus'`, CurrentPath, AdbName)
	res, err := ExecPowerCmdChina(call)
	Log.Println("CurrentFocus", res, err)
	return res
}
